PDF for UWP | ComponentOne
Using PDF for UWP / Specifying Page Sizes and Orientation
In This Topic
    Specifying Page Sizes and Orientation
    In This Topic

    You may have noticed that in the previous examples, we started adding content to the document right after creating the C1PdfDocument object. This is possible because when you create the C1PdfDocument, it automatically adds an empty page to the document, ready to receive any type of content.

    When you are done filling up the first page, you can add a new one using the C1PdfDocumentBase.NewPage method.

    By default, all pages in the document have the same size and orientation. These parameters can be specified in the C1PdfDocument constructor. You can also change the page size and orientation at any time by setting the C1PdfDocument.PaperKindC1PdfDocument.PageSize, and C1PdfDocument.Landscape properties. For example, the code below creates a document with all paper sizes defined by the PaperKind enumeration:

    VB
    Copy Code
    Dim rect As New Rect(72, 72, 100, 50)
    ' create constant font and StringFormat objects
    Dim font As New Font("Tahoma", 18)
    Dim sf As New StringFormat()
    sf.Alignment = HorizontalAlignment.Center
    sf.LineAlignment = VerticalAlignment.Center
    ' create one page with each paper size
    Dim firstPage As Boolean = True
    For Each fi As var In GetType(PaperKind).GetFields(System.Reflection.BindingFlags.[Static] Or System.Reflection.BindingFlags.[Public])
        ' Silverlight/Phone doesn't have Enum.GetValues
        Dim pk As PaperKind = DirectCast(fi.GetValue(Nothing), PaperKind)
        ' skip custom size
        If pk = PaperKind.[Custom] Then
            Continue For
        End If
        ' add new page for every page after the first one
        If Not firstPage Then
            pdf.NewPage()
        End If
        firstPage = False
        ' set paper kind and orientation
        pdf.PaperKind = pk
        pdf.Landscape = Not pdf.Landscape
        ' draw some content on the page
        Dim text As String = String.Format("PaperKind: [{0}];" & vbCr & vbLf & "Landscape: [{1}];" & vbCr & vbLf & "Font: [Tahoma 18pt]", pdf.PaperKind, pdf.Landscape)
        pdf.DrawString(text, font, Colors.Black, rect, sf)
        pdf.DrawRectangle(Colors.Black, rect)
    Next
    
    C#
    Copy Code
    Rect rect = new Rect(72, 72, 100, 50);
    // create constant font and StringFormat objects
    Font font = new Font("Tahoma", 18);
    StringFormat sf = new StringFormat();
    sf.Alignment = HorizontalAlignment.Center;
    sf.LineAlignment = VerticalAlignment.Center;
    
    // create one page with each paper size
    bool firstPage = true;
    foreach (var fi in typeof(PaperKind).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public))
    {
        // Silverlight/Phone doesn't have Enum.GetValues
        PaperKind pk = (PaperKind)fi.GetValue(null);
    
        // skip custom size
        if (pk == PaperKind.Custom) continue;
    
        // add new page for every page after the first one
        if (!firstPage) pdf.NewPage();
        firstPage = false;
    
        // set paper kind and orientation
        pdf.PaperKind = pk;
        pdf.Landscape = !pdf.Landscape;
    
        // draw some content on the page
    
        string text = string.Format("PaperKind: [{0}];\r\nLandscape: [{1}];\r\nFont: [Tahoma 18pt]",
            pdf.PaperKind, pdf.Landscape);
        pdf.DrawString(text, font, Colors.Black, rect, sf);
        pdf.DrawRectangle(Colors.Black, rect);
    }
    

    You are not restricted to writing on the last page that was added to the document. You can use the C1PdfDocument.CurrentPage property to select which page you want to write to, and then use the regular drawing commands as usual. This is useful for adding content to pages after you are done rendering a document. For example, the code below adds footers to each page containing the current page number and the total of pages in the document (page n of m):

    VB
    Copy Code
    Dim font As New Font("Tahoma", 7, PdfFontStyle.Bold)
    Dim sf As New StringFormat()
    sf.Alignment = HorizontalAlignment.Center
    For page As Integer = 0 To pdf.Pages.Count - 1
        ' Select page.
        pdf.CurrentPage = page
        ' Build rectangle for rendering the footer.
        Dim rect As Rect = pdf.PageRectangle
        rect.Y = rect.Bottom - 36
        ' Write the footer.
        Dim text As String = String.Format("Page {0} of {1}", page + 1, pdf.Pages.Count)
        pdf.DrawString(text, font, Colors.Gray, rect, sf)
    Next
    
    C#
    Copy Code
    Font font = new Font("Tahoma", 7, PdfFontStyle.Bold);
    
    StringFormat sf = new StringFormat();
    sf.Alignment = HorizontalAlignment.Center;
    for (int page = 0; page < pdf.Pages.Count; page++)
    {
        // Select page.
        pdf.CurrentPage = page;
        // Build rectangle for rendering the footer.
    
        Rect rect = pdf.PageRectangle;
    
        rect.Y = rect.Bottom - 36;
        // Write the footer.
        string text = string.Format("Page {0} of {1}", page + 1, pdf.Pages.Count);
        pdf.DrawString(text, font, Colors.Gray, rect, sf);
    }
    

    Note that the code uses the C1PdfDocumentBase.Pages property to get the page count. Pages is a collection based on the ArrayList class, and has methods that allow you to count and enumerate pages, as well as add and remove pages at specific positions. You can use the Pages collection to remove pages from certain locations in the document and re-insert them elsewhere.